/* model */
import Field from "@model/entity/Field"
import { FieldTypeMappingEnum } from "@model/enum/FieldMappingEnum"
/* types */
import { ShbDingtalkAssistantDoc } from "@gpt/views/edit/dingtalk-assistant/types"
import ComponentNameEnum from "@model/enum/ComponentNameEnum"
import { dispatch } from "@src/util/emitter"
import { isNull, isUndefined } from "@src/util/type"

enum SettingDingtalkDocFieldNameEnum {
  Doc = 'doc',
  Study = 'study',
  Operate = 'operate',
}

enum SettingGPTDingtalkAssistantDocTableEventNameEnum {
  Edit = 'edit',
  Delete = 'delete',
}

const SettingDingtalkDocField: Field = {
  displayName: "钉钉知识库",
  fieldName: SettingDingtalkDocFieldNameEnum.Doc,
  formType: FieldTypeMappingEnum.Text,
  isNull: Number(false),
  renderCell: (h, column, row: ShbDingtalkAssistantDoc) => {
    return (
      <div>
        { row.docUrl }
      </div>
    )
  }
}

const SettingDingtalkStudyField: Field = {
  displayName: "学习状态",
  fieldName: SettingDingtalkDocFieldNameEnum.Study,
  formType: FieldTypeMappingEnum.Text,
  isNull: Number(false),
  renderCell: (h, column, row: ShbDingtalkAssistantDoc) => {
    
    const docNum = row.docNum
    const studyNum = row.studyNum as number
    const learningNum = row.learningNum
    const failNum = row.failNum
    
    if (isUndefined(docNum) || isUndefined(studyNum)) {
      return (<div></div>)
    }
    
    if (isNull(docNum) || isNull(studyNum)) {
      return (<div></div>)
    }
    
    const isInStudy = studyNum == 0
    
    if (isInStudy) {
      return (
        <div class="setting-gpt-dingtalk-assistant-doc-table__study-status--learning">
          <div class="setting-gpt-dingtalk-assistant-doc-table__study-status--learning__icon">
          </div>
          学习中
        </div>
      )
    }
    
    function getTooltipContent() {
      
      const isAllLearned = studyNum == docNum
      
      if (isAllLearned) {
        return '已学习 ' + docNum + ' 个文档'
      }
      
      const isAllLearning = learningNum == docNum
      if (isAllLearning) {
        return '学习中 ' + learningNum + ' 个文档'
      }
      
      return (
        '已学习 ' + studyNum + ' 个文档' + '，' 
        + '学习中 ' + learningNum + ' 个文档' + '，' 
        + '失败 ' + failNum + ' 个文档'
        + '，共 ' + docNum + ' 个文档'
      )
    }
    
    const tooltipContent = getTooltipContent()
    
    return (
      <div class="setting-gpt-dingtalk-assistant-doc-table__study-status--learned">
        
        <div class="setting-gpt-dingtalk-assistant-doc-table__study-status--learned__icon">
        </div>
        
        <span class="setting-gpt-dingtalk-assistant-doc-table__study-status--learned__text">
          已学习
          <span>
            ({ studyNum }/{ docNum })
          </span>
        </span>
        
        <span>
          <el-tooltip
            placement="top"
            content={ tooltipContent }
          >
            <i class="iconfont icon-info-circle"></i>
          </el-tooltip>
        </span>
        
      </div>
    )
  }
}

const SettingDingtalkOperateField: Field = {
  displayName: "操作",
  fieldName: SettingDingtalkDocFieldNameEnum.Operate,
  formType: FieldTypeMappingEnum.Text,
  isNull: Number(false),
  renderCell: (h, column, row: ShbDingtalkAssistantDoc, tableInstance) => {
    
    const robotTableComponentName = ComponentNameEnum.SettingGPTDingtalkAssistantDocTable
    
    const dispatchEvent = (event: string, ...args: any[]) => {
      dispatch.call(tableInstance, robotTableComponentName, event, ...args)
    }
    
    const editHandler = () => {
      dispatchEvent(SettingGPTDingtalkAssistantDocTableEventNameEnum.Edit, row)
    }
    
    const deleteHandler = () => {
      dispatchEvent(SettingGPTDingtalkAssistantDocTableEventNameEnum.Delete, row)
    }
    
    const editButton = (
      <el-button
        type="text"
        onClick={editHandler}
      >
        编辑
      </el-button>
    )
    
    const deleteButton = (
      <el-button
        type="text"
        onClick={deleteHandler}
      >
        删除
      </el-button>
    )
    
    return (
      <div>
        { editButton }
        { deleteButton }
      </div>
    )
  }
}

const SettingDingtalkDocFields = [
  SettingDingtalkDocField,
  SettingDingtalkStudyField,
  SettingDingtalkOperateField,
]

export {
  SettingDingtalkDocFieldNameEnum,
  SettingDingtalkDocField,
  SettingDingtalkStudyField,
  SettingDingtalkOperateField,
  SettingDingtalkDocFields,
}